package com.threeti.mecool.core.application.acl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.threeti.mecool.core.domain.model.acl.Orgnization;
import com.threeti.mecool.core.domain.model.acl.Employee;
import com.threeti.mecool.core.util.TreeHelper;
import com.threeti.mecool.legacy.domain.model.acl.OrgnizationEmployeeLegacy;
import com.threeti.mecool.legacy.domain.model.acl.OrgnizationLegacy;
import com.threeti.mecool.legacy.infrastructure.persistence.OrgnizationEmployeeLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.OrgnizationLegacyRepository;

@Service
public class EmployeeServiceImpl implements EmployeeService {
  
  @Autowired
  OrgnizationEmployeeLegacyRepository orgEmpLegacyRepo;
  
  @Autowired
  OrgnizationLegacyRepository orgLegacyRepo;

  @Override
  public List<Orgnization> findAllOrgnizations() {
    List<OrgnizationLegacy> orgLegs = orgLegacyRepo.findAll();
    
    List<Orgnization> resultTopLevelNodes = TreeHelper.templateBuildTree(orgLegs, Orgnization.class, orgLegacyRepo);
    
    return resultTopLevelNodes;
  }

  @Override
  public List<Orgnization> findAllChildOrgnizationsBy(String parentOrgCode) {
    List<OrgnizationLegacy> orgLegs = orgLegacyRepo.findByParentCode(parentOrgCode);
    
    List<Orgnization> resultNodes = TreeHelper.templateBuildTree(orgLegs, Orgnization.class, orgLegacyRepo, parentOrgCode);
    return resultNodes;
  }

  @Override
  public List<Employee> findAllChildEmployeesBy(String orgCode) {
    List<OrgnizationEmployeeLegacy> orgLegs = orgEmpLegacyRepo.findByOrgCode(orgCode);
    
    List<Employee> resultNodes = TreeHelper.templateBuildTree(orgLegs, Employee.class, orgEmpLegacyRepo);
    return resultNodes;
  }

}
